-- | Regression test

module Test (main) where

import qualified BsBB
import qualified BB
import qualified Bs
import qualified Ply
import qualified Iv
import Interlude
import Test.HUnit
import Test.QuickCheck as QC

main :: IO ()
main = qc >> runTestTT tests >> return ()

qc :: IO ()
qc = do
  putStr "min bs is lower bound:  "
  quickCheckWith (QC.stdArgs {maxSize=25}) $ \(p::Ply.T Rational) ->
               QC.forAll (QC.vectorOf (Ply.vars p) (QC.choose (0,1))) $ \x ->
                 Bs.lo (Bs.ofPly p) <= Ply.eval p x

  putStr "eval and ofPly commute: "
  quickCheckWith (QC.stdArgs {maxSize=25}) $ \(p::Ply.T Rational) ->
               QC.forAll (QC.vector (Ply.vars p)) $ \x ->
                 Bs.eval (Bs.ofPly p :: Bs.T Rational) x == Ply.eval p x

  putStr "eval and (*) commute:   "
  quickCheckWith (QC.stdArgs {maxSize=25}) $ \p1 (p2 :: Ply.T Rational) ->
    QC.vector (max (Ply.vars p1) (Ply.vars p2)) >>= \x ->
      return $ Ply.eval (p1*p2) x == Ply.eval p1 x * Ply.eval p2 x

  putStr "eval and (+) commute:   "
  quickCheckWith (QC.stdArgs {maxSize=25}) $ \p1 (p2 :: Ply.T Rational) ->
    QC.vector (max (Ply.vars p1) (Ply.vars p2)) >>= \x ->
      return $ Ply.eval (p1+p2) x == Ply.eval p1 x + Ply.eval p2 x

tests :: Test
tests = TestList $
  map (\(e,lu) -> TestLabel e $ TestCase $ do
           s <- readFile ("examples/" ++ e)
           lu' <- writeStdIO $ BsBB.prepare (-1) s
                  >>= BB.inf (BsBB.trm $ Just (0.0001::Double)) (-1)
           lu' @?= lu)
    [ ("beale",            Iv.mk (-6.302305500744587e-5) 3.297212740501e-5)
    , ("booth",            Iv.mk (-5.340576171875e-5)    8.58306884765625e-6)
    , ("camelback",        Iv.mk (-3.0949288079362787)   (-3.0948380875801083))
    , ("ferguson",         Iv.mk 2887.3557358971593      2887.3557358971593)
    , ("goldstein_price",  Iv.mk 2.999948501586914       3.0)
    , ("rosenbrock",       Iv.mk (-9.541337688763936e-5) 0.0)
    , ("bug046",           Iv.mk (-0.6485739755735267)   (-0.6485629174712813))
    , ("bug087",           Iv.mk 0                       0)
    , ("colville",         Iv.mk (-8.616052400611807e-5) 1.3822748535119445e-5)
    , ("gamma_pt",         Iv.mk 0                       0)
    ]
